import {format} from 'date-fns';
import md5 from 'md5';
import { encode } from 'js-base64';
import request from '@/service/fetch';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function sendVerifyCode (req:NextApiRequest,res:NextApiResponse){
    const {to='',templateId="1"} = req.body
    console.log('aaa')
    console.log(to)
    console.log(templateId)
    const AppId = '2c94811c946f6bfb01952d9931c51a80'
    const AccountId = '2c94811c946f6bfb01952d992ffe1a79'
    const AuthToken = '31065dd3b9054784978af0e7477535c4'
    const NowDate = format(new Date(), 'yyyyMMddHHmmss');
    const SigParameter = md5(`${AccountId}${AuthToken}${NowDate}`);
    const Authorization = encode(`${AccountId}:${NowDate}`);
    const verifyCode = Math.floor(Math.random() * (9999 - 1000)) + 1000;
    const expireMinute = '5';
    const url = `https://app.cloopen.com:8883/2013-12-26/Accounts/${AccountId}/SMS/TemplateSMS?sig=${SigParameter}`;
    const response = await request.post(
        url,
        {
            to,
            templateId,
            appId: AppId,
            datas: [verifyCode, expireMinute],
        },
        {
            headers: {
                Authorization,
            },
        }
    );

    console.log(verifyCode)
    console.log(response);
}
